<include file="public@header_vue"/>
</head>
<style>
    .el-pagination {
        margin-top: 20px;
    }


    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .photos {
        width: 120px;
        height: 120px;
        border-radius: 6px;
        overflow: hidden;
        float: left;
        margin-right: 10px;
        margin-bottom: 10px;
        position: relative;
    }

    .photos-options {
        position: absolute;
        top: 0;
        left: 0;
        height: 120px;
        line-height: 120px;
        width: 120px;
        text-align: center;
        background-color: rgba(0, 0, 0, 0.6);
        z-index: 10;
        opacity: 0;
        transition: opacity 0.6s;
    }

    .photos:hover .photos-options {
        opacity: 1;
    }

    .photos-options .el-button {
        font-size: 24px;
        color: white
    }

    .photos img {
        width: 120px;
        height: 120px;
    }

    .photos-uploader {
        width: 120px;
        height: 120px;
        float: left;
    }

    .logo-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .logo-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
</style>
<body>
<div id="app">
    <el-tabs type="border-card">
        <el-tab-pane label="模板信息列表" v-loading="onDataLoading">
            <el-row>
                <el-col :span="24">
                    <el-form :inline="true" :model="form" size="mini">

                        <el-form-item>
                            <el-button type="primary" @click="update(null)">新增模板消息</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="template_title" label="模板标题">
                </el-table-column>
                <el-table-column v-if="{$role_id}!=3" prop="company_info.name" label="公司名称">
                </el-table-column>
                <el-table-column prop="template_content" label="模板内容"></el-table-column>
                <el-table-column prop="template_id" label="模板id"></el-table-column>
                <el-table-column prop="status" label="是否启用">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.status"
                                active-color="#13ce66"
                                :active-value="1"
                                :inactive-value="0"
                                @change="change_status('status',scope.row.status,scope.row.id)"
                                inactive-color="#ff4949">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="create_time" label="添加时间" min-width="120">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.create_time)}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="update(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="total,prev, pager, next" :total="totalNum" :page-size="10"
                           :current-page.sync="currentPage" @current-change="getData"></el-pagination>
        </el-tab-pane>
    </el-tabs>
    <el-dialog :title="sourceForm.id == '' ? '新增模板消息' : '修改模板消息'" :visible.sync="dialogVisible" width="60%"
               v-loading.fullscreen="dialogLoading">
        <el-form :model="sourceForm" class="demo-form-inline" :rules="rules" ref="sourceForm"
                 label-width="200px">
            <el-form-item label="模板标题" prop="template_title">
                <el-col :span="18">
                    <el-input v-model="sourceForm.template_title" placeholder="模板标题"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item v-if="{$role_id}!=3" label="选择公司" prop="company_id">
                <el-col :span="18">
                    <el-select v-model="sourceForm.company_id" placeholder="选择公司">
                        <el-option
                                v-for="item in companyData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-col>
            </el-form-item>
            <el-form-item label="模板id" prop="template_id">
                <el-col :span="18">
                    <el-input v-model="sourceForm.template_id" placeholder="模板id"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="模板内容" prop="template_content">
                <el-col :span="18">
                    <el-input type="textarea" v-model="sourceForm.template_content" placeholder="模板内容"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="是否启用" prop="status">
                <el-col :span="18">
                    <el-switch
                            v-model="sourceForm.status"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            :active-value="1"
                            :inactive-value="0">
                    </el-switch>
                </el-col>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button v-if="sourceForm.id === ''" type="primary" @click="submit('sourceForm')">新增</el-button>
            <el-button v-else type="primary" @click="submit('sourceForm')">修改</el-button>
        </span>
    </el-dialog>

</div>
</body>
<script>
    var app = new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [],
                companyData: [],
                form: {
                    name: '',
                },
                sourceForm: {
                    id: '',
                    company_id: '',
                    template_id: '',
                    status: 0,
                    template_title: '',
                    template_content: '',

                },
                totalNum: 0,
                currentPage: 1,
                dialogLoading: false,
                onDataLoading: false,
                durationOptions: {
                    shortcuts: [{
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                dialogVisible: false,
                rules: {
                    template_title: [{required: true, message: '请输入模板标题', trigger: 'blur'}],
                    company_id: [{required: true, message: '请选择公司', trigger: 'blur'}],
                    template_id: [{required: true, message: '请输入模板id', trigger: 'blur'}],
                    template_content: [{required: true, message: '请输入模板内容', trigger: 'blur'}],
                },
                headers: {'X-Requested-With': 'XMLHttpRequest'},
            }
        },
        created() {
            this.getCompanyData();
            this.getData()
        },
        methods: {
            clearForm() {
                this.form = {
                    name: '',
                }
            },
            getData(page = 1) {
                let _this = this;
                let params = {page: page};
                this.onDataLoading = true;
                if (this.form.duration !== '') {
                    params.duration = this.form.duration;
                }
                axios.get("/plugin/wechat/admin_index/template_message", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data.data;
                        _this.totalNum = response.data.data.total;
                        console.log(_this.totalNum)
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            getCompanyData() {
                let _this = this;
                axios.get("{:url('admin/company/get_company_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.companyData = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            submit(form) {
                let _this = this;
                _this.$refs[form].validate((valid) => {
                    if (valid) {
                        axios.post("/plugin/wechat/admin_index/template_save", this.sourceForm, {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest',
                            }
                        }).then(function (response) {
                            _this.dialogLoading = false;
                            if (response.data.code === 1) {
                                if (_this.sourceForm.id === '') {
                                    _this.$message.success('新增完成');
                                } else {
                                    _this.$message.success('修改成功');
                                }
                                _this.getData();
                                _this.dialogVisible = false;
                            } else {
                                _this.$message.warning(response.data.msg);
                            }
                        }).catch(function (error) {
                            _this.dialogLoading = false;
                            _this.$message.error(error.toString())
                        })
                    } else {
                        _this.dialogLoading = false;
                        return false;
                    }
                });
            },
            update(data) {
                let _this = this;
                if (data) {
                    console.log(data)
                    _this.sourceForm = JSON.parse(JSON.stringify(data));
                    console.log(_this.sourceForm)
                } else {
                    _this.sourceForm = {
                        id: '',
                        company_id: '',
                        template_id: '',
                        template_title: '',
                        template_content: '',
                        status: 0,
                    }
                }
                _this.dialogVisible = true;
                _this.dialogLoading = false;
            },
            change_status(field, value, id) {
                let _this = this;
                this.dialogLoading = true;
                let params = {field: field, value: value, id: id};
                axios.post("/plugin/wechat/admin_index/change_status", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('修改完成');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                })
            },
            formatDate(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp * 1000);
                    return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
                } else {
                    return '';
                }
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        }
    });
</script>
